
#pragma used+
sfrb PINB=3;
sfrb DDRB=4;
sfrb PORTB=5;
sfrb PINC=6;
sfrb DDRC=7;
sfrb PORTC=8;
sfrb PIND=9;
sfrb DDRD=0xa;
sfrb PORTD=0xb;
sfrb TIFR0=0x15;
sfrb TIFR1=0x16;
sfrb TIFR2=0x17;
sfrb PCIFR=0x1b;
sfrb EIFR=0x1c;
sfrb EIMSK=0x1d;
sfrb GPIOR0=0x1e;
sfrb EECR=0x1f;
sfrb EEDR=0x20;
sfrb EEARL=0x21;
sfrb EEARH=0x22;
sfrw EEAR=0x21;   
sfrb GTCCR=0x23;
sfrb TCCR0A=0x24;
sfrb TCCR0B=0x25;
sfrb TCNT0=0x26;
sfrb OCR0A=0x27;
sfrb OCR0B=0x28;
sfrb GPIOR1=0x2a;
sfrb GPIOR2=0x2b;
sfrb SPCR=0x2c;
sfrb SPSR=0x2d;
sfrb SPDR=0x2e;
sfrb ACSR=0x30;
sfrb SMCR=0x33;
sfrb MCUSR=0x34;
sfrb MCUCR=0x35;
sfrb SPMCSR=0x37;
sfrb SPL=0x3d;
sfrb SPH=0x3e;
sfrb SREG=0x3f;
#pragma used-

#asm
	#ifndef __SLEEP_DEFINED__
	#define __SLEEP_DEFINED__
	.EQU __se_bit=0x01
	.EQU __sm_mask=0x0E
	.EQU __sm_adc_noise_red=0x02
	.EQU __sm_powerdown=0x04
	.EQU __sm_powersave=0x06
	.EQU __sm_standby=0x0C
	.SET power_ctrl_reg=smcr
	#endif
#endasm

#pragma used+

void delay_us(unsigned int n);
void delay_ms(unsigned int n);

#pragma used-

typedef unsigned char       BOOL;

typedef unsigned char       BYTE;
typedef unsigned short      WORD;
typedef unsigned long       DWORD;

typedef unsigned char       UINT8;
typedef unsigned short      UINT16;
typedef unsigned long       UINT32;

typedef signed char         INT8;
typedef signed short        INT16;
typedef signed long         INT32;

void halSpiStrobe(BYTE strobe);

BYTE halSpiReadStatus(BYTE addr);

void halSpiWriteReg(BYTE addr, BYTE value);

BYTE halSpiReadReg(BYTE addr);

void halSpiWriteBurstReg(BYTE addr, BYTE *buffer, BYTE count);

void halSpiReadBurstReg(BYTE addr, BYTE *buffer, BYTE count);

typedef struct S_RF_SETTINGS{
BYTE FSCTRL1;   
BYTE FSCTRL0;   
BYTE FREQ2;     
BYTE FREQ1;     
BYTE FREQ0;     
BYTE MDMCFG4;   
BYTE MDMCFG3;   
BYTE MDMCFG2;   
BYTE MDMCFG1;   
BYTE MDMCFG0;   
BYTE CHANNR;    
BYTE DEVIATN;   
BYTE FREND1;    
BYTE FREND0;    
BYTE MCSM0;     
BYTE FOCCFG;    
BYTE BSCFG;     
BYTE AGCCTRL2;  
BYTE AGCCTRL1;  
BYTE AGCCTRL0;  
BYTE FSCAL3;    
BYTE FSCAL2;    
BYTE FSCAL1;    
BYTE FSCAL0;    
BYTE FSTEST;    
BYTE TEST2;     
BYTE TEST1;     
BYTE TEST0;     
BYTE IOCFG2;    
BYTE IOCFG0;    
BYTE PKTCTRL1;  
BYTE PKTCTRL0;  
BYTE ADDR;      
BYTE PKTLEN;    
} RF_SETTINGS;

void halRfWriteRfSettings(flash RF_SETTINGS *pRfSettings);

void halRfSendPacket(BYTE *txBuffer, UINT8 size);

#pragma used+

unsigned char cabs(signed char x);
unsigned int abs(int x);
unsigned long labs(long x);
float fabs(float x);
int atoi(char *str);
long int atol(char *str);
float atof(char *str);
void itoa(int n,char *str);
void ltoa(long int n,char *str);
void ftoa(float n,unsigned char decimals,char *str);
void ftoe(float n,unsigned char decimals,char *str);
void srand(int seed);
int rand(void);
void *malloc(unsigned int size);
void *calloc(unsigned int num, unsigned int size);
void *realloc(void *ptr, unsigned int size); 
void free(void *ptr);

#pragma used-
#pragma library stdlib.lib

extern void cc1100_init (void);
extern BOOL RfReceivePacket(BYTE *rxBuffer, UINT8 *length);

unsigned char rf_err;
volatile unsigned char uart_tx_len;
volatile unsigned char uart_rx_len;
volatile unsigned char uart_rx_flag;
volatile unsigned char uart_rx_point;
volatile unsigned char uart_rx_finish;
volatile unsigned char rf_sent;
unsigned char uart_tx_data[20];
unsigned char uart_rx_data[20];

interrupt [19] void usart_rx_isr(void)
{
unsigned char status,data;
status=(*(unsigned char *) 0xc0);
data=(*(unsigned char *) 0xc6);

if ((status & ((1<<4) | (1<<2) | (1<<3)))==0)
{
if(rf_sent)
{
if(uart_rx_flag)
{
uart_rx_point=uart_rx_point+1;
uart_rx_data[uart_rx_point]=data;
if(uart_rx_point==(uart_rx_data[1]+1))
{
if(PORTC.2                  ){PORTC.2                  =0;}else{PORTC.2                  =1;}
uart_rx_flag = 0;
uart_rx_len = uart_rx_data[1]+2;
uart_rx_finish =1;
rf_sent =0;
}

}else if(data == 0xa5)
{
uart_rx_flag = 1;
uart_rx_data[0]= data;
uart_rx_point = 0;
uart_rx_finish = 0;
}                
}
}
}  

void CHIP_INT(void) {

#pragma optsize-
(*(unsigned char *) 0x61)=0x80;
(*(unsigned char *) 0x61)=0x00;

PORTB=0x00;
DDRB=0x2D;

PORTC=0x0C;
DDRC=0x0C;

PORTD=0x00;
DDRD=0xE0;

TCCR0A=0x00;
TCCR0B=0x00;
TCNT0=0x00;
OCR0A=0x00;
OCR0B=0x00;

(*(unsigned char *) 0x80)=0x00;
(*(unsigned char *) 0x81)=0x00;
(*(unsigned char *) 0x85)=0x00;
(*(unsigned char *) 0x84)=0x00;
(*(unsigned char *) 0x87)=0x00;
(*(unsigned char *) 0x86)=0x00;
(*(unsigned char *) 0x89)=0x00;
(*(unsigned char *) 0x88)=0x00;
(*(unsigned char *) 0x8b)=0x00;
(*(unsigned char *) 0x8a)=0x00;

(*(unsigned char *) 0xb6)=0x00;
(*(unsigned char *) 0xb0)=0x00;
(*(unsigned char *) 0xb1)=0x00;
(*(unsigned char *) 0xb2)=0x00;
(*(unsigned char *) 0xb3)=0x00;
(*(unsigned char *) 0xb4)=0x00;

(*(unsigned char *) 0x69)=0x00;
EIMSK=0x00;
(*(unsigned char *) 0x68)=0x00;

(*(unsigned char *) 0x6e)=0x00;

(*(unsigned char *) 0x6f)=0x00;

(*(unsigned char *) 0x70)=0x00;

(*(unsigned char *) 0xc0)=0x00;
(*(unsigned char *) 0xc1)=0x98;
(*(unsigned char *) 0xc2)=0x06;
(*(unsigned char *) 0xc5)=0x00;
(*(unsigned char *) 0xc4)=0x05;

ACSR=0x80;
(*(unsigned char *) 0x7b)=0x00;

SPCR=0x50;
SPSR=0x00;
}

void uart_tx(unsigned char * tx_data_p,unsigned char tx_data_len)
{ 
unsigned char tmp;
if(PORTC.2                  ){PORTC.2                  =0;}else{PORTC.2                  =1;}
for(tmp=0;tmp<tx_data_len;tmp++)
{
while(!((*(unsigned char *) 0xc0) & (1<<5)));
(*(unsigned char *) 0xc6) = * tx_data_p;
tx_data_p++;
}
}

void error_process(void){
if (rf_err == 1) {
cc1100_init();
rf_err = 0;
}
}

void main(void) {

CHIP_INT();
cc1100_init();

rf_err = 0;
uart_tx_len = 0;
uart_rx_len = 0;
uart_rx_flag = 0;
uart_rx_point = 0;
uart_rx_finish = 0;
rf_sent = 1;        
PORTC.2                  = 0;
PORTC.3                  = 0; 

#asm("sei");    

while(1)
{

error_process();
if(uart_rx_finish)
{
halRfSendPacket(uart_rx_data,uart_rx_len);
if(PORTC.3                  ){PORTC.3                  =0;}else{PORTC.3                  =1;}
uart_rx_finish =0;
rf_sent = 1;
}

uart_tx_len = 15;
if(RfReceivePacket(uart_tx_data, &uart_tx_len))
{
uart_tx(uart_tx_data,uart_tx_len);                                         
}

}

}
